<?php

namespace app\models;

use app\components\helpers\MappingHelper;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\db\Exception;
use yii\db\Query;
use yii\helpers\Json;

/**
 * @property int $id 主键
 * @property string $key 角色ID
 * @property string $name 名称
 * @property int $status 状态
 * @property string $remark 备注
 * @property int $is_super 超级角色
 * @property int $author_id 作者
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Role extends ActiveRecord
{
    const IS_SUPER_NO = 0;
    const IS_SUPER_YES = 1;

    public static function tableName()
    {
        return '{{%role}}';
    }


    public function behaviors()
    {
        return [['class' => TimestampBehavior::class]];
    }

    public function rules()
    {
        return [[['key', 'name', 'status', 'remark', 'is_super', 'author_id'], 'safe']];
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'author_id']);
    }


    /**
     * 查询角色列表 以权限项ID 为条件
     * @param $param
     * @return array
     */
    public static function getListByItemId($param)
    {
        $query = new Query;
        $query->select([
            'r.name AS role',
            'u.username AS author',
            'FROM_UNIXTIME(r.created_at, "%Y-%m-%d %H:%i:%S") AS createdAt',
            'i.name AS item',
            'r.id AS roleId'
        ]);
        $query->from('{{%role_item}} ri');
        $query->leftJoin('{{%role}} r', 'r.id=ri.role_id');
        $query->leftJoin('{{%item}} i', 'i.id=ri.item_id');
        $query->leftJoin('{{%user}} u', 'u.id=r.author_id');
        $query->andFilterWhere(['like', 'r.name', $param['name']]);
        $query->andFilterWhere(['ri.item_id' => $param['id']]);

        $pagination = new Pagination([
            'defaultPageSize' => 10,
            'totalCount' => $query->count()
        ]);

        $data = $query->offset($pagination->getOffset())
            ->orderBy('r.id DESC')
            ->limit($pagination->getLimit())->all();

        $dataProvider = new ArrayDataProvider([
            'models' => $data,
            'Pagination' => $pagination,
        ]);

        $role = $dataProvider->getModels();
        $pageSize = $pagination->getPageSize();
        $totalPage = $pagination->getPageCount();
        $totalCount = $pagination->totalCount;

        return [
            'role' => $role,
            'totalCount' => (int)$totalCount,
            'pageSize' => $pageSize,
            'totalPage' => $totalPage,
        ];
    }

    /**
     * Notes:角色联表查询
     * Date: 2020/7/24
     * Time: 10:45
     * @param $params
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getJoinList($params)
    {
        $query = static::find();
        $query->alias('r');
        if (isset($params['select'])) {
            $query->select($params['select']);
        } else {
            $query->select(['r.id']);
        }
        if (isset($params['User'])) {
            $query->leftJoin(User::tableName() . 'u', 'u.id = r.author_id');
        }
        if (isset($params['pageSize'])) {
            static::setPaging($query, $params['pageSize']);
        }
        return $query->asArray()->all();
    }

}
